<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2018/2/28
 * Time: 下午6:57
 */

namespace App;


trait ApiResponse
{

    function api(string $msg, $code = 200, $data = [],$token="")
    {
        $result = ["code" => $code, "msg" => $msg];
        if (!empty($data)) {
            $result['data'] = $data;
        }
        if($token){
            $result['token'] = $token;
        }
        if ($code == 401) {
            return response()->json($result)->setStatusCode(401);
        } else {
            return response()->json($result)->setStatusCode(200);
        }
    }

}